package restaurant.iterators;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import restaurant.Comment;
import restaurant.Rating;

/**
 * An iterator to view the ratings for an item.
 * 
 * @author Odessa
 */

public class RatingsIterator implements Iterator
{
	/** The item number to view the ratings for. */
	private int itemNo;

	/** The iterator through the list of comments. */
	private java.util.Iterator<Comment> com_iterator;

	/** The next rating for this item. */
	private Rating nextComment;



	/**
	 * Instantiates a new by ratings iterator.
	 * 
	 * @param comments the list of comments.
	 * @param itemNo the item number to view the comments for.
	 */
	public RatingsIterator(ArrayList<Comment> comments, int itemNo) {
		this.itemNo = itemNo;
		com_iterator = comments.iterator();
	}



	/**
	 * Determines if there is another rating for this item.
	 * 
	 * @return {@code true} if there is another rating for this item.
	 * @see restaurant.iterators.Iterator#hasNext()
	 */
	@Override
	public boolean hasNext()
	{
		boolean matchFound = false;

		while (com_iterator.hasNext()) {
			Rating temp = (Rating) com_iterator.next();

			if (temp.getItemNo() == itemNo) {
				matchFound = true;
				nextComment = temp;
				break;
			}
		}

		if (matchFound == true) {
		}
		else
			nextComment = null;

		return matchFound;
	}



	/**
	 * Returns the next rating for this item.
	 * 
	 * @return the next rating for this item.
	 * @throws NoSuchElementException if there aren't any more ratings for this
	 *             item.
	 * @see restaurant.iterators.Iterator#next()
	 */
	@Override
	public Object next() throws NoSuchElementException
	{
		if (nextComment == null) {
			throw new NoSuchElementException();
		}
		else
			return nextComment;
	}

}
